﻿using System;
using System.Windows;
using nRoute.Components;
using nRoute.Components.Composition;
using nRoute.Mapping;
using nRoute.ViewModels;
using nRoute.ViewServices.Contracts;
using Officer2013.Content.Model;
using Officer2013.Content.Views;
using Officer2013.Infrastructure.Components;

namespace Officer2013.Content.ViewModels
{
    [DefineNavigationContent("Dialogs/OptionsDialog", typeof(OptionsDialog))]
    [MapViewModel(typeof(OptionsDialog))]
    public class OptionsDialogViewModel 
        : NavigationViewModelBase
    {

#region Constants and Variables

        private const string PENDING_CHANGES = "Pending Changes";
        private const string CONFIRM_PENDING_CHANGED = "There are outstanding changes to your Options. Press Ok to proceed without saving, or Cancel to cancel navigation";
        private const string ADVANCED_SETTINGS_KEY = "AdvancedSettingsKey";
        private const string COMMON_SETTINGS_KEY = "CommonSettingsKey";

        private bool _isDirty;
        private readonly IShowMessageViewService _showMessageViewService;

#endregion

        public OptionsDialogViewModel(IShowMessageViewService showMessageViewService)
        {
            _showMessageViewService = showMessageViewService;

            SetupCommnads();
            LoadOptionSettings();
        }

#region Properties

        public ActionCommand SaveCommand { get; private set; }

        public ActionCommand CancelCommand { get; private set; }

        public AdvancedSettings AdvancedSettings { get; private set;  }

        public CommonSettings CommonSettings { get; private set; }

#endregion

#region NavigationState Related

        protected override void OnClosing(Action<bool> confirmCallback)
        {
            if (_isDirty)
            {
                _showMessageViewService.ShowMessage(PENDING_CHANGES, CONFIRM_PENDING_CHANGED, true, (b) => confirmCallback(b.GetValueOrDefault(false)));
            }
            else
            {
                base.OnClosing(confirmCallback);
            }
        }

#endregion

#region Helpers

        private void SetupCommnads()
        {
            SaveCommand = new ActionCommand(() =>
            {
                IsolatedStore.SaveDataObject(this.AdvancedSettings, ADVANCED_SETTINGS_KEY);
                IsolatedStore.SaveDataObject(this.CommonSettings, COMMON_SETTINGS_KEY);
                _isDirty = false;
            });

            CancelCommand = new ActionCommand(() =>
            {
                LoadOptionSettings();
                _isDirty = false;
            });
        }

        private void LoadOptionSettings()
        {
            // we get from the isolated store and notify property changed
            var _cachedAdvancedSettings = IsolatedStore.LoadDataObject<AdvancedSettings>(ADVANCED_SETTINGS_KEY) ?? new AdvancedSettings();
            AdvancedSettings = _cachedAdvancedSettings;
            NotifyPropertyChanged(() => AdvancedSettings);

            // we get from the isolated store and notify property changed
            var _cachedCommonSettings = IsolatedStore.LoadDataObject<CommonSettings>(COMMON_SETTINGS_KEY) ?? new CommonSettings();
            CommonSettings = _cachedCommonSettings;
            NotifyPropertyChanged(() => CommonSettings);

            // we listen for changes
            AdvancedSettings.PropertyChanged += (s, e) => _isDirty = true;
            CommonSettings.PropertyChanged += (s, e) => _isDirty = true;
        }

#endregion

    }
}
